<template>
  <div id="ATable">
    <div class="ATable-btn">
      <el-row class="mb-4">
        <el-button tag="primary" plain @click="tagadd">新增</el-button>
        <el-button tag="primary" plain>导入</el-button>
        <el-button tag="primary" plain>导出</el-button>
        <el-input v-model="tagSearch"  placeholder="输入关键词" clearable />
        <el-button tag="primary" plain @click="load">查询</el-button>
      </el-row>
    </div>
    <div class="ATable-content">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="标签ID" width="150"/>
        <el-table-column prop="name" label="标签名称" width="150"/>
        <el-table-column prop="blogId" label="博客Id" width="150"/>
<!--        <el-table-column prop=" " label=" " width="150"/>-->
<!--        <el-table-column prop=" " label=" " width="150"/>-->
<!--        <el-table-column prop=" " label=" " width="150"/>-->
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button tag="primary" plain @click="tagEdit(scope.$index,scope.row)">编辑</el-button>
            <el-button tag="primary" plain @click="tagDelete(scope.$index,scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <div class="demo-pagination-block">
        <span class="demonstration"></span>
        <el-pagination
            v-model:currentPage="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        >
        </el-pagination>


        <el-dialog
            v-model="dialogVisible"
            title="提示"
            width="30%"
        >
          <el-form ref="formRef" :model="tagform" label-width="120px">
            <el-form-item label="类型名称">
              <el-input v-model="tagform.name"></el-input>
            </el-form-item>
            <el-form-item label="博客ID">
              <el-input v-model="tagform.blogId"></el-input>
            </el-form-item>
          </el-form>
          <template #footer>
              <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button tag="primary" @click="tagsave"
                >确定</el-button
                >
              </span>
          </template>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import request from "../../utils/request";

export default {
  name: 'Blog',

  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: '',
      pageSize: 10,
      tagSearch: '',
      dialogVisible: false,
      tagform: {
        id:'',
        name: '',
        blogId: ''
      },
    }
  },

  created() {
    this.load()
  },

  methods: {
    load() {
      request.get("/tag", {
        params:{
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.tagSearch
        }

      }).then(res => {
        this.tableData = res.data.records,
            this.total = res.data.total
      })
    },
    tagEdit(index,row){
      this.tagform = JSON.parse(JSON.stringify(row))
      console.log(this.tagform)
      this.dialogVisible = true
    },
    tagDelete(index,id){
      request.delete("/tag/delete",{
        params:{
          id
        }
      }).then(res =>{
        this.load()
      })

    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      this.load()
    },
    tagadd() {
      this.dialogVisible = true
      this.tagform = {}
    },
    tagsave() {
      if (this.tagform.id) {  // 更新
        request.put("/tag/update", this.tagform).then(res => {
          console.log(res)
          this.load() // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      } else {  // 新增
        request.post("/tag/save", this.tagform).then(res => {
          console.log(res)
          this.load() // 刷新表格的数据
          this.dialogVisible = false  // 关闭弹窗
        })
      }
    }
  }

}
</script>

<style lang="less" scoped>
@import "../../style/admin/table.less";
</style>
